// pages/login/login.js

const app = getApp()
const db = wx.cloud.database().collection('userList')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        name: "",
        no: "",
        user: null
    },
    //获取输入姓名
    getNameInput(e) {
        this.setData({
            name: e.detail
        })
        var name = this.data.name
        console.log("name is : " + name)
    },
    //获取输入工号
    getNoInput(e) {
        this.setData({
            no: e.detail
        })
        var no = this.data.no
        console.log("no is : " + no);
    },
    //用户登录
    userLog() {
        var name = this.data.name
        var no = this.data.no

        db.get({
            success: (res) => {
                for (let i = 0; i < res.data.length; i++) {
                    if (name == res.data[i].name && no == res.data[i].no) {
                        wx.showToast({
                                title: '登录成功',
                                icon: "success"
                            }),
                            wx.setStorageSync('user', res.data[i])
                        wx.navigateTo({
                            url: '/pages/home/home?name=' + name + '&no=' + no,
                        })
                        break;
                    } else {
                        wx.showToast({
                            title: '姓名或工号错误',
                            icon: "error"
                        })
                    }
                }

            },
            fail: (res) => {
                console.log(res);
            }
        })
    },

    isLogin() {

        var user = wx.getStorageSync('user')
        if (user) {
            console.log("用户已登录");
            var name = user.name
            var no = user.no
            wx.navigateTo({
                url: '/pages/home/home?name=' + name + '&no=' + no,
            })
        }else{
            console.log("用户未登录或退出登录");
        }
    },

    onLoad() {
        this.isLogin()
    }
})